﻿package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.badibadi.adapter.InformAdapter;
import com.badibadi.infos.JuBao_Models;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
/*
 * 用户举报接口----POST
 * @param $jubao = array(
 *                      $uid,    //(必需)主动举报的用户ID
 *                    //  $b_uid,    //(必需)被举报的内容所属的用户ID
 *                      $table,    //(必需)举报的类型（humor_trends：心情动态，photo：相册，record：记录（篇），record_article：记录（记），activity：活动，club：俱乐部，discuss：评论）
 *                      $tid,    //(必需)被举报的内容ID
 *                      $b_cid,    //若是举报俱乐部中的内容，那么俱乐部ID（个人时$b_cid=0或不传）
 *                      $jb_typeid,    //(必需)举报类别ID
 *                      $reasons,    //（必需）举报原因
 *                      )
 */
public class InformActivity extends BaseActivity implements OnClickListener {

	private GridView mgv;
	private InformAdapter informAdapter;
	private Results results;
	private String data;
	private String type;//获取举报的类型jubao
	private String bei_person_id;// 被举报的人的Id
	private String bei_neirong_id;// 被举报的内容的id
	private String bei_cid;// 被举报的俱乐部的id(选传)
	private String table;//举报的类型
	private JuBao_Models bao_Models;
	private TextView inform_bucong_neirong;
	// private List<Users> list;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case 1:
					Utils.ExitPrgress(InformActivity.this);
					Utils.showMessage(InformActivity.this, getResources().getString(R.string.l_net_error));
					break;
				case 2:
					Utils.ExitPrgress(InformActivity.this);
					try {
						informAdapter = new InformAdapter(InformActivity.this,
								bao_Models.getLists());
						mgv.setAdapter(informAdapter);
					} catch (Exception e) {
						// TODO: handle exception
					}
					
					break;
				case 3:
					Utils.ExitPrgress(InformActivity.this);
					Utils.showMessage(InformActivity.this,getResources().getString(R.string.SystemError));
					break;
				case 4:
					Utils.ExitPrgress(InformActivity.this);
					Utils.showMessage(InformActivity.this, getResources().getString(R.string.l_xb55));
					break;
				default:
					break;
				}
			} catch (Exception e) {
				   
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.inform);
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		
	     type = "jubao";

	 	try {
			table = getIntent().getStringExtra("table");//举报种类
		} catch (Exception e) {
			   
		}
		try {
			bei_person_id = getIntent().getStringExtra("bei_person_id");//被举报人id
		} catch (Exception e) {
			   
		}
		try {
			bei_neirong_id = getIntent().getStringExtra("bei_neirong_id");//被举报内容id
		} catch (Exception e) {
			   
		}
		try {
			bei_cid = getIntent().getStringExtra("bei_cid");// 被举报的俱乐部的id(选传)
		} catch (Exception e) {
			   
		}
	
//		   
		
		
		
		this.findViewById(R.id.tijiao).setOnClickListener(InformActivity.this);
		this.findViewById(R.id.inform_Return_btn).setOnClickListener(this);
		inform_bucong_neirong = (TextView) findViewById(R.id.inform_bucong_neirong);
		mgv = (GridView) findViewById(R.id.chose_inform_type);
		listType(type,
				com.badibadi.mytools.Dialog
						.getSystemLanguageType(InformActivity.this));
	}

	private void listType(final String type, final String languageType) {
		Utils.showPrgress(InformActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", type);
				map.put("languageType", languageType);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "type/listType");
//				   ("举报类型获取传入数据：" + map.toString());
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						try {
//							   ("获取的举报类型的数据是:" + content);
							bao_Models = new JuBao_Models();
							bao_Models = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), JuBao_Models.class);
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	private void Jubao_(final String uid, final String b_uid,
			final String table, final String tid, final String b_cid,
			final String jb_typeid, final String reasons) {
		Utils.showPrgress(InformActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				try {
					map.put("jubao", fengzhuang(uid,b_uid,table,tid,b_cid,jb_typeid,reasons));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					   
				}
				
	
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "commonality/jubao");
			
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
//					   ("我是返回的举报类容"+results.getRetmsg());
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						try {
//							   ("获取的举报类型的数据是:" + content + " "
//									+ results.getRetmsg());
							handler.sendEmptyMessage(4);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}

		
		});
	}
	private JSONObject fengzhuang(String uid, String b_uid, String table,
			String tid, String b_cid, String jb_typeid, String reasons) throws JSONException {
		// TODO Auto-generated method stub
//		jubao
//		map.put("uid", uid);// 主动举报的用户id
//		map.put("b_uid", b_uid);// 被举报的用户id
//		map.put("table", table);// 举报的类型
//		map.put("tid", tid);// 被举报的内容ID
//		map.put("b_cid", b_cid);// 被举报的俱乐部ID
//		map.put("jb_typeid", jb_typeid);// 举报类别ID
//		map.put("reasons", reasons);// 举报原因
		JSONObject js=new JSONObject();
		js.put("uid", uid);
		js.put("b_uid", b_uid);
		js.put("table", table);
		js.put("tid", tid);
		js.put("b_cid", b_cid);
		js.put("jb_typeid",jb_typeid);
		js.put("reasons", reasons);
		
		return js;
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.inform_Return_btn:
			InformActivity.this.finish();
			break;
		case R.id.tijiao:
			//    
			data = informAdapter.getData();// 举报类别的id
			String bucong = inform_bucong_neirong.getText().toString().trim();
			if (data != null) {
				if (bucong != null) {
					if (bei_person_id != null && bei_neirong_id != null) {
						Jubao_(Utils.getUid(InformActivity.this), bei_person_id, table, bei_neirong_id,
								bei_cid, data, bucong);
					} else {
						Toast.makeText(getApplicationContext(),
								getResources().getString(R.string.l_xb74), 0).show();
					}
				} else {
					Toast.makeText(getApplicationContext(), getResources().getString(R.string.l_xb75), 0)
							.show();
				}
			} else {
				Toast.makeText(getApplicationContext(), getResources().getString(R.string.l_xb76), 0).show();
			}
			break;
		}
	}
}
